<?php
require 'common.php';

class AdminController extends CommonController {
    function __construct()
    {
      parent::__construct();

        $this->setTemplateDir(SITE_PATH . '/templates/admin/');
    }

    function index()
    {
        $key = isset($_POST["key"])?$_POST["key"]:"";

        $articles =  $this->getAll("select article.*,tag.name as tag_name from article left join tag on
               article.tag_id=tag.id where article.name like '%$key%'");

        $this->assign("key",$key);
        $this->assign("articles",$articles);

        $this->display("article.html");

    }
    function add(){
        if($_POST){


                $tag_id = $_POST["tagId"];
                $name = $_POST["name"];

                $content = $_POST["content"];

                $time = time();

              $this->  query("insert into article (name,content,time,tag_id) values('$name','$content','$time','$tag_id')");
           $this->    redirect("admin.php", "添加成功！");

        }else{
            $this->display("add.html");
        }
    }

    function del(){
        $id = $_GET["id"];
        $this->query("delete from article where id ='$id'");
        $this->    redirect("admin.php", "删除成功！");
    }

    function edit(){
            if($_POST){
                $id =$_POST["id"];
                $name =$_POST["name"];
                $tagId = $_POST["tagId"];
                $content =$_POST["content"];

              $this-> query("update article set name='$name',tag_id='$tagId',content='$content' where id='$id'");
                $this-> redirect("admin.php","更新成功！");

            }else{
                $id = $_GET["id"];

                $article = $this->getOne("select article.*,tag.name as tag_name from article left join tag on article.tag_id= tag.id
             where article.id='$id'");


                $this->assign("article", $article);

                $this->display("edit.html");
            }
    }

    function tag(){
            $this->display("tag.html");
    }
    function delTag(){
        $id=$_GET["id"];
        $this->query("delete from tag where id='$id'");
        $this->redirect("admin.php?action=tag","删除成功!");
    }
    function addTag(){
        if($_POST){
            $name =$_POST["name"];
            $this-> query("insert into tag (name) values('$name')");
            $this-> redirect("admin.php?action=tag","新增成功！");
        }else{
            $this->display("add_tag.html");
        }
    }
    function editTag(){
        if($_POST){
            $name =$_POST["name"];
            $tagId = $_POST["tagId"];
            $this-> query("update tag set name='$name'where id='$tagId'");
            $this-> redirect("admin.php?action=tag","更新成功！");

        }else{
            $id = $_GET["id"];
            $tag = $this->getOne("select *  from tag t where t.id='$id'");
            $this->assign("tag", $tag);
            $this->display("edit_tag.html");
        }
    }

}

$action = isset($_GET["action"])? $_GET["action"] :"index";

$admin = new AdminController();

$admin->$action();